Python Pandas Index.append()用法及代码示例

您所在的位置:网站首页 words append Python Pandas Index.append()用法及代码示例

Python Pandas Index.append()用法及代码示例

2024-07-16 19:10| 来源: 网络整理| 查看: 265

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。

Pandas Index.append()函数用于将单个或一组索引附加在一起。在收集索引的情况下,所有索引都按照传递给索引的相同顺序附加到原始索引中。Index.append()函数。该函数返回附加索引。

用法: Index.append(other)

参数: other:索引或索引列表/元组

返回:appended:bool或数组(如果指定了轴)单个元素数组可以转换为bool。

范例1:采用Index.append()函数将单个索引附加到给定索引。

# importing pandas as pd import pandas as pd    # Creating the first Index df1 = pd.Index([17, 69, 33, 5, 0, 74, 0])    # Creating the second Index df2 = pd.Index([11, 16, 54, 58])    # Print the first and second Index print(df1, "\n", df2)

输出:

让我们在df1的末尾附加df2索引。

# append df2 at the end of df1 df1.append(df2)

输出:

正如我们在输出中看到的那样,第二个索引即df2已附加在df1的末尾。范例2:使用Index.append()函数在给定索引的末尾追加索引集合。

# importing pandas as pd import pandas as pd    # Creating the first Index df1 = pd.Index(['Jan', 'Feb', 'Mar', 'Apr'])    # Creating the second Index df2 = pd.Index(['May', 'Jun', 'Jul', 'Aug'])    # Creating the third Index df3 = pd.Index(['Sep', 'Oct', 'Nov', 'Dec'])    # Print the first, second and third Index print(df1, "\n", df2, "\n", df3)

输出:

让我们在df1的末尾同时添加索引df2和df3。

# We pass df2 and df3 as a list of # indexes to the append function df1.append([df2, df3])

输出:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3